<?php
// +-------------------------------
// | 中润报表系统
// +-------------------------------
// | author: zw
// +-------------------------------

namespace app\adminapi\controller\report;

use app\adminapi\controller\BaseAdminController;
use think\facade\Db;

/**
 * 数据分析控制器
 * Class AnalysisController
 * @package app\adminapi\controller\report
 */
class AnalysisController extends BaseAdminController
{
    public $tables = [
        1=>"ylmglqk",
        2=>"pmzbjjtzlqk",
        4=>"jlscjgykzqk",
        5=>"gxlscjgykzqk",
        6=>"jhscjgykzqk",
        7=>"dlscqk",
        8=>"jcscqk",
        9=>"jbscqk",
    ];
    /**
     * @notes  获取报表分析
     * @return \think\response\Json
     * @author zw
     * @date 2023/2/1 13:24
     */
    public function getSubmitAnalysis()
    {
        // 接受所有参数
        $data = input('param.');
        $tablename = $this->tables[$data['type']];
        $obj = Db::name($tablename);
        $where = [];
        // 根据时间类型  拼接时间条件
        if($data['date']=='day'){
            !empty($data['start'])||$data['start'] = date("Y-m-d",strtotime("-1 day"));
            !empty($data['end'])||$data['end'] = date("Y-m-d",strtotime("-1 day"));
            $where[] = ['date','>=',$data['start']];
            $where[] = ['date','<=',$data['end']];
        }elseif($data['date']=='month'){
            if(empty($data['start'])){
                $data['start'] = date("Y-m");
            }
            $where[] = ['date','like','%'.$data['start'].'%'];
        }elseif($data['date']=='year'){
            if(empty($data['start'])){
                $data['start'] = date("Y");
            }
            $where[] = ['date','like','%'.$data['start'].'%'];
        }
        $where[] = ['status','=','2'];
        $table_columns = [];
        $countobj = [];
        $svgobj = [];
        $count = 0;
        $arr = [];
        $countnumber = 0;
        $svgnumber = 0;
        // 原料煤管理情况
        if($data['type']==1){
            $table_columns = [
                "date"=>"时间",
                "kczl"=>"库存总量(t)",
                "mccl"=>"煤场存量(t)",
                "jnmcl"=>"局内煤存量(t)",
                "jczl"=>"日进厂煤合计(t)",
            ];
            // 拿到所有数据 进行计算
            $arr = $obj->field('date,sum(asl) a,sum(bsl) b,sum(csl) c,sum(dsl) d,jnmcl,sum(jczl) jczl')
                    ->where($where)
                    ->group('date')
                    ->select()
                    ->toArray();
            // echo Db::getLastSql();die;
            $pm = Db::name('pmzbjjtzlqk')->field('date,sum(cl) cl')->where($where)->group('date')->select()->toArray();
            
            // 煤场存量
            $count = count($arr);
            if(!empty($arr)){
                foreach ($arr as $key => $value) {
                    $arr[$key]['mccl'] = round((int)$value['a']+(int)$value['b']+(int)$value['c']+(int)$value['d'],2);
                    if(!empty($pm)){
                        foreach ($pm as $k => $v) {
                            if($v['date']==$value['date']){
                                // 库存总量=煤场总量+配煤表里面的存量的和
                                $arr[$key]['kczl'] = round((int)$arr[$key]['mccl']+(int)$v['cl'],2);
                                $countnumber += $arr[$key]['jczl'];
                            }
                        }
                    }else{
                        $arr[$key]['kczl'] = round((int)$arr[$key]['mccl'],2);
                        $countnumber += $arr[$key]['jczl'];
                    }
                }
                $countnumber = round($countnumber,2);
                $svgnumber = round(($countnumber/$count),2);;
            }
            $countobj = ['累计进厂量',$countnumber];
            $svgobj = ['平均进厂量',$svgnumber];
        }elseif($data['type']==2){//配煤指标及焦炭质量情况
            $table_columns = [
                "date"=>"时间",
                "rhl"=>"日耗量(t)",
                "qumei"=>"取煤(t)",
                "jdkhmq"=>"解冻库秏煤气(万m³)",
            ];
            // 拿到所有数据 进行计算
            $arr = $obj->field('date,sum(rhl) rhl,qumei,jdkhmq')
                    ->where($where)
                    ->group('date')
                    ->select()
                    ->toArray();
            // echo Db::getLastSql();die;
            $count = count($arr);
            if(!empty($arr)){
                foreach ($arr as $key => $value) {
                    $countnumber += $value['rhl'];
                }
                $countnumber = round($countnumber,2);
                $svgnumber = round(($countnumber/$count),2);;
            }
            $countobj = ['累计日耗量',$countnumber];
            $svgobj = ['平均日耗量',$svgnumber];
        }elseif($data['type']==4){//焦炉生产及工艺控制情况
            $table_columns = [
                "date"=>"时间",
                "jhls"=>"计划炉数",
                "sjls"=>"实际炉数",
                "dlzml"=>"单炉干基装煤量(t)",
                "hlmql"=>"回炉煤气量(万m³)",
            ];
            $arr = $obj->field('date,sum(jhls) jhls,sum(sjls) sjls,avg(dlzml) dlzml,sum(hlmql) hlmql')
                    ->where($where)
                    ->group('date')
                    ->select()
                    ->toArray();
            
            $count = count($arr);
            if(!empty($arr)){
                foreach ($arr as $key => $value) {
                    $countnumber += $value['sjls'];
                }
                $countnumber = round($countnumber,2);
                $svgnumber = round(($countnumber/$count),2);;
            }
            $countobj = ['累计炉数',$countnumber];
            $svgobj = ['平均炉数',$svgnumber];
        }elseif($data['type']==5){//干熄炉生产及工艺控制情况
            $table_columns = [
                "date"=>"时间",
                "gjrcl"=>"干焦日产量",
                "wdrhl"=>"污氮日耗量",
                "onefdl"=>"汽机1#日发电量（万kw·h）",
                "twofdl"=>"汽机2#日发电量（万kw·h）",
                "wsrwsl"=>"外送蒸汽日外送量（t）",
                "liuliang"=>"蒸汽流量(t)",
                // "hlmql"=>"冶金焦率",
                // "hlmql"=>"月平均",
            ];
            $arr = $obj->field('date,sum(gjrcl) gjrcl,sum(wdrhl) wdrhl,onefdl,twofdl,wsrwsl,sum(liuliang) liuliang')
                    ->where($where)
                    ->group('date')
                    ->select()
                    ->toArray();
            
            $count = count($arr);
            if(!empty($arr)){
                foreach ($arr as $key => $value) {
                    $countnumber += $value['gjrcl'];
                }
                $countnumber = round($countnumber,2);
                $svgnumber = round(($countnumber/$count),2);;
            }
            $countobj = ['累计干焦产量',$countnumber];
            $svgobj = ['平均干焦产量',$svgnumber];
        }elseif($data['type']==6){//净化生产及工艺控制情况
            $table_columns = [
                "date"=>"时间",
                "meiqir"=>"煤气（万m³）",
                "jiaoyour"=>"焦油（t）",
                "liuanr"=>"硫铵（t）",
                "cubenr"=>"粗苯（t）",
                "liuhuangr"=>"硫磺（t）",
                "fuyanr"=>"副盐（t）",
                "xshr"=>"污水新生化处理量（t）",
                "lshr"=>"污水老生化处理量（t）",
            ];
            $arr = $obj->field('date,meiqir,jiaoyour,liuanr,cubenr,liuhuangr,fuyanr,xshr,lshr')
                    ->where($where)
                    ->group('date')
                    ->select()
                    ->toArray();
            
            $count = count($arr);
            if(!empty($arr)){
                foreach ($arr as $key => $value) {
                    $countnumber += $value['meiqir'];
                }
                $countnumber = round($countnumber,2);
                $svgnumber = round(($countnumber/$count),2);;
            }
            $countobj = ['累计煤气产量',$countnumber];
            $svgobj = ['平均煤气产量',$svgnumber];
        }elseif($data['type']==7){//动力生产情况
            $table_columns = [
                "date"=>"时间",
                "ydlr"=>"日用电量（万kw·h）",
                "ysr"=>"日雨水排量",
                "yuanshui"=>"原水",
                "mqcs4"=>"4#煤气掺烧（万m3）",
                "mqcs5"=>"5#煤气掺烧（万m3）",
                "mqcshj"=>"合计煤气掺烧（万m3）",
                "diya"=>"低压",
                "rzjl"=>"日总计量",
                "rzqcl"=>"日蒸汽产量（t）",
                "rhml"=>"日耗煤量（t）",
            ];
            $arr = $obj->field('date,ydlr,ysr,yuanshui,mqcs4,mqcs5,mqcshj,diya,rzjl,(zq40rc+zq75rc) as rzqcl,(hm40rh+hm75rh) as rhml')
                    ->where($where)
                    ->group('date')
                    ->select()
                    ->toArray();
            $count = count($arr);
            if(!empty($arr)){
                foreach ($arr as $key => $value) {
                    $countnumber += $value['rzqcl'];
                }
                $countnumber = round($countnumber,2);
                $svgnumber = round(($countnumber/$count),2);;
            }
            $countobj = ['累计蒸汽产量',$countnumber];
            $svgobj = ['平均蒸汽产量',$svgnumber];
        }elseif($data['type']==8){//甲醇生产情况
            $table_columns = [
                "date"=>"时间",
                "jcr"=>"甲醇日产量（t）",
                "hmr"=>"日耗煤气量（万m³）",
                "mqll"=>"湿脱硫出口平均煤气流量（m³/h）",
            ];
            $arr = $obj->field('date,jcr,hmr,mqll')
                    ->where($where)
                    ->group('date')
                    ->select()
                    ->toArray();
            $count = count($arr);
            if(!empty($arr)){
                foreach ($arr as $key => $value) {
                    $countnumber += $value['jcr'];
                }
                $countnumber = round($countnumber,2);
                $svgnumber = round(($countnumber/$count),2);;
            }
            $countobj = ['累计甲醇产量',$countnumber];
            $svgobj = ['平均甲醇产量',$svgnumber];
        }elseif($data['type']==9){//精苯生产情况
            $table_columns = [
                "date"=>"时间",
                "cbjglr"=>"粗苯日加工量（t）",
                "cbsclr"=>"纯苯日产生量（t）",
                "hcfqlr"=>"日耗驰放气量（万m³）",
                "qita78"=>"甲苯7# 8#（t）",
                "qita9"=>"二甲苯9#（t）",
                "qita13"=>"重苯13#（t）",
                "qita12"=>"环戊烷12#（t）",
                "qita10"=>"C8+10#（t）",
            ];
            $arr = $obj->field('date,cbjglr,cbsclr,hcfqlr,qita78,qita9,qita13,qita12,qita10')
                    ->where($where)
                    ->group('date')
                    ->select()
                    ->toArray();
            $count = count($arr);
            if(!empty($arr)){
                foreach ($arr as $key => $value) {
                    $countnumber += $value['cbjglr'];
                }
                $countnumber = round($countnumber,2);
                $svgnumber = round(($countnumber/$count),2);;
            }
            $countobj = ['累计粗苯加工量',$countnumber];
            $svgobj = ['平均粗苯加工量',$svgnumber];
        }
        return $this->data([
            "countobj"=>$countobj,
            "svgobj"=>$svgobj,
            "table_columns"=>$table_columns,
            "count"=>$count,
            "lists"=>$arr?:[],
            "page_no"=>$data['page_no'],
            "page_size"=>$data['page_size'],
        ]);
    }
    /**
     * @notes  获取库存分析
     * @return \think\response\Json
     * @author zw
     * @date 2023/2/1 13:24
     */
    public function getStockAnalysis()
    {
        // 接受所有参数
        $data = input('param.');
        $obj = Db::name('stock');
        // 根据时间类型  拼接时间条件
        if($data['date']=='day'){
            !empty($data['start'])||$data['start'] = date("Y-m-d",strtotime("-1 day"));
            !empty($data['end'])||$data['end'] = date("Y-m-d",strtotime("-1 day"));
            $obj = $obj->where('date','>=',$data['start']);
            $obj = $obj->where('date','<=',$data['end']);
        }elseif($data['date']=='month'){
            if(empty($data['start'])){
                $data['start'] = date("Y-m",strtotime("-1 month"));
            }
            $obj = $obj->where('date','like','%'.$data['start'].'%');
        }elseif($data['date']=='year'){
            if(empty($data['start'])){
                $data['start'] = date("Y",strtotime("-1 year"));
            }
            $obj = $obj->where('date','like','%'.$data['start'].'%');
        }
        // 药剂类型
        if(!empty($data['type'])){
            $obj = $obj->where('name|pname','=',$data['type']);
        }

        // 分厂
        if(!empty($data['company'])){
            $obj = $obj->where('company','=',$data['company']);
        }
        $obj = $obj->where('status','=','2');
        $count = $obj->count();

        // 拿到所有数据 进行计算
        $arr = $obj->select()->toArray();
        $countnumber = 0;
        $svgnumber = 0;
        if(!empty($arr)){
            foreach ($arr as $key => $value) {
                $countnumber += (int)$value['dryl'];
            }
            $countnumber = round($countnumber,2);
            $svgnumber = round(($countnumber/$count),2);
        }

        $offset = ($data['page_no']-1)*$data['page_size'];
        $is = $obj->limit($offset,$data['page_size'])->select()->toArray();
        // echo Db::getLastSql();die;
        return $this->data([
            "countnumber"=>$countnumber,
            "svgnumber"=>$svgnumber,
            "count"=>$count,
            "lists"=>$is?:[],
            "page_no"=>$data['page_no'],
            "page_size"=>$data['page_size'],
        ]);
    }
}